<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Storage;

class ImageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $imgs = DB::table('images')->get();
        //dd($imgs);
        return view('home.image_index')->with('imgs',$imgs);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //普通form文件上传
        //return view('home.image_add');
        //ajax文件上传
        return view('home.ajax_image_add');

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //单图片上传
       /* $img = $request->file('img');
        //确认上传的文件是否成功
        if($img->isValid()){
            //$picname = $file->getClientOriginalName(); //获取上传原文件名
            $ext = $img->getClientOriginalExtension(); //获取上传文件名的后缀名
            //执行移动上传文件
            $path = public_path('upload/images/');
            //$filename = "thumb_".time().rand(10,100).$ext;  缩略图
            $imgname = "origin_".rand(10,10000).".".$ext;
            if($img->move($path,$imgname)){
                $url = $path.$imgname;
                DB::table('images')->insert(['url'=>$url]);
            }else{
                return redirect('image');
            }
        }*/

        //多图片上传
        /*$imgs = $request->file('img');
        $urls = [];
        foreach($imgs as $img) {
            if ($img->isValid()) {
                $ext = $img->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $path = 'upload/images/';
                //$filename = "thumb_".time().rand(10,100).$ext;  缩略图
                $imgname = "origin_" . time().rand(10, 10000) . '.' . $ext;
                if ($img->move($path, $imgname)) {
                    $url = '/upload/images/'.$imgname;
                    $urls[] = ['url' => $url];
                }
            }
        }
        DB::table('images')->insert($urls);
        return redirect('image');*/

        //文件存储方式上传
        $imgs = $request->file('img');
        $urls = [];
        foreach($imgs as $img) {
            if ($img->isValid()) {
                $ext = $img->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $path = 'images/';
                //$filename = "thumb_".time().rand(10,100).$ext;  缩略图
                $imgname = "origin_".time().rand(10, 10000).'.'.$ext;
                $path .= $imgname;
                //获取文件上传到服务器之后的临时路径
                $tmp_path = $img->getRealPath();
                //利用文件存储执行上传 put($path,file_get_contents($tmp_path))
                Storage::disk('myupload')->put($path,file_get_contents($tmp_path));
                //把上传之后的url加入数组
                $urls[] = ['url' => '/upload/'.$path];
            }
        }
        DB::table('images')->insert($urls);
        //return redirect('image');

        //返回ajax
        $data = [
            'status' => 200,
            'msg'    => '成功',
            'data'   => $urls
        ];
        echo json_encode($data);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
